package com.example.bilibili.service.impl;

import com.example.bilibili.dao.BvideoDao;
import com.example.bilibili.entity.Bvideo;
import com.example.bilibili.service.BvideoService;
import com.example.bilibili.utils.IDUtils;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageImpl;
import org.springframework.data.domain.PageRequest;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

/**
 * (Bvideo)表服务实现类
 *
 * @author makejava
 * @since 2022-08-16 20:29:29
 */
@Service("bvideoService")
public class BvideoServiceImpl implements BvideoService {
    @Resource
    private BvideoDao bvideoDao;

    /**
     * 通过ID查询单条数据
     *
     * @param id 主键
     * @return 实例对象
     */
    @Override
    public Bvideo queryById(String id) {
        return this.bvideoDao.queryById(id);
    }

    /**
     * 分页查询
     *
     * @param bvideo      筛选条件
     * @param pageRequest 分页对象
     * @return 查询结果
     */
    @Override
    public Page<Bvideo> queryByPage(Bvideo bvideo, PageRequest pageRequest) {
        long total = this.bvideoDao.count(bvideo);
        String sort = '`' + pageRequest.getSort().toString().replace(':', '`');
        return new PageImpl<>(this.bvideoDao.queryAllByLimit(bvideo, pageRequest, sort), pageRequest, total);
    }

    /**
     * 新增数据
     *
     * @param bvideo 实例对象
     * @return 实例对象
     */
    @Override
    public Bvideo insert(Bvideo bvideo) {
        bvideo.setId(IDUtils.getStringID());
        this.bvideoDao.insert(bvideo);
        return bvideo;
    }

    @Override
    public void insertList(List<Bvideo> list) {
        if (list == null || list.isEmpty()) {
            return;
        }
        bvideoDao.insertBatch(list);
    }


    /**
     * 修改数据
     *
     * @param bvideo 实例对象
     * @return 实例对象
     */
    @Override
    public Bvideo update(Bvideo bvideo) {
        this.bvideoDao.update(bvideo);
        return this.queryById(bvideo.getId());
    }

    /**
     * 通过主键删除数据
     *
     * @param id 主键
     * @return 是否成功
     */
    @Override
    public boolean deleteById(String id) {
        return this.bvideoDao.deleteById(id) > 0;
    }
}
